<!-- components/Modal/Modal.vue -->
<template>
  <teleport to="body">
    <transition name="modal-fade">
      <div
          v-if="visible"
          class="modal-mask"
          @click.self="handleOverlayClick"
          @keydown.esc="handleEsc"
          role="dialog"
          aria-modal="true"
          :aria-labelledby="title ? 'modal-title' : null"
      >
        <div
            class="modal-container"
            :class="{ centered }"
            :style="{
            width: typeof width === 'number' ? `${width}px` : width,
            height: typeof height === 'number' ? `${height}px` : height
          }"
        >
          <div class="modal-header">
            <slot name="header">
              <h2 v-if="title" id="modal-title" class="modal-title">{{ title }}</h2>
            </slot>
            <button
                v-if="showCloseButton"
                class="modal-close"
                @click="closeModal"
                aria-label="Close"
            >
              &times;
            </button>
          </div>

          <div class="modal-content">
            <slot name="content">
              <div v-if="content" class="modal-default-content">{{ content }}</div>
            </slot>
          </div>

          <div v-if="showFooter" class="modal-footer">
            <slot name="footer">
              <button
                  v-if="showCancelButton"
                  class="modal-button cancel-button"
                  @click="handleCancel"
              >
                {{ cancelText }}
              </button>
              <button
                  v-if="showOkButton"
                  class="modal-button ok-button"
                  @click="handleOk"
              >
                {{ okText }}
              </button>
            </slot>
          </div>
        </div>
      </div>
    </transition>
  </teleport>
</template>

<script setup>
import { computed, onMounted, onBeforeUnmount } from 'vue'

const emit = defineEmits([
  'update:visible',
  'ok',
  'cancel',
  'open',
  'close'
])

const props = defineProps({
  visible: {
    type: Boolean,
    default: false
  },
  closeOnClickOverlay: {
    type: Boolean,
    default: true
  },
  closeOnEsc: {
    type: Boolean,
    default: true
  },
  title: {
    type: String,
    default: ''
  },
  content: {
    type: String,
    default: ''
  },
  width: {
    type: [String, Number],
    default: '500px'
  },
  height: {
    type: [String, Number],
    default: 'auto'
  },
  centered: {
    type: Boolean,
    default: true
  },
  showOkButton: {
    type: Boolean,
    default: true
  },
  showCancelButton: {
    type: Boolean,
    default: true
  },
  okText: {
    type: String,
    default: '确定'
  },
  cancelText: {
    type: String,
    default: '取消'
  },
  showCloseButton: {
    type: Boolean,
    default: true
  }
})

const showFooter = computed(() =>
    props.showOkButton || props.showCancelButton
)

const handleOverlayClick = () => {
  if (props.closeOnClickOverlay) {
    closeModal()
  }
}

const handleEsc = (event) => {
  if (props.closeOnEsc && event.key === 'Escape') {
    closeModal()
  }
}

const handleOk = () => {
  emit('ok')
  closeModal()
}

const handleCancel = () => {
  emit('cancel')
  closeModal()
}

const closeModal = () => {
  emit('update:visible', false)
  emit('close')
}

const openModal = () => {
  emit('open')
}

const handleKeydown = (e) => {
  if (props.visible) {
    handleEsc(e)
  }
}

onMounted(() => {
  window.addEventListener('keydown', handleKeydown)
})

onBeforeUnmount(() => {
  window.removeEventListener('keydown', handleKeydown)
})
</script>

<style scoped>
.modal-fade-enter-active,
.modal-fade-leave-active {
  transition: opacity 0.3s ease;
}

.modal-fade-enter-from,
.modal-fade-leave-to {
  opacity: 0;
}

.modal-mask {
  position: fixed;
  z-index: 9998;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  transition: opacity 0.3s ease;
}

.modal-container {
  margin: auto;
  background-color: #fff;
  border-radius: 4px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.33);
  transition: all 0.3s ease;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.modal-container.centered {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

.modal-header {
  padding: 16px;
  border-bottom: 1px solid #eee;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.modal-title {
  margin: 0;
  font-size: 1.25rem;
}

.modal-close {
  cursor: pointer;
  border: none;
  background: none;
  font-size: 1.5rem;
  padding: 0 8px;
}

.modal-content {
  padding: 16px;
  flex: 1;
  overflow-y: auto;
}

.modal-footer {
  padding: 16px;
  border-top: 1px solid #eee;
  display: flex;
  justify-content: flex-end;
  gap: 8px;
}

.modal-button {
  padding: 8px 16px;
  border-radius: 4px;
  cursor: pointer;
  border: 1px solid #ddd;
}

.ok-button {
  background-color: #007bff;
  color: white;
  border-color: #007bff;
}

.cancel-button {
  background-color: #6c757d;
  color: white;
  border-color: #6c757d;
}
</style>